package com.app.micaihu.view.home.knowledge.presenter

import com.app.base.mvp.BasePresenter
import com.app.http.NetRequestListener
import com.app.http.bean.BaseBean
import com.app.micaihu.bean.news.VideoList
import com.app.micaihu.configure.LoadType
import com.app.micaihu.utils.ApiServiceUtils.apiService
import com.app.micaihu.utils.ParamsMapUtils
import com.app.micaihu.view.home.knowledge.contract.KnowledgeContract
import com.app.micaihu.view.home.questions.contract.QuestionsContract

/**
 * @author
 * @date 2020
 */
open class KnowledgePresenter : BasePresenter<KnowledgeContract.View>(), KnowledgeContract.Presenter {

    private var isRefresh = true
    private var page = 1
    private var firstLoad = true

    override fun videoList() {
        if (firstLoad) {
            getView()?.showLoading()
            firstLoad = false
        }

        val params = ParamsMapUtils.commonParams
        params["page"] = page
        requestNetwork(apiService.knowledge(params), object : NetRequestListener<BaseBean<VideoList?>?> {
            override fun onSuccess(result: BaseBean<VideoList?>?) {
                getView()?.hideLoading()
                page++
                val loadType = if (isRefresh) LoadType.TYPE_REFRESH_SUCCESS else LoadType.TYPE_LOAD_MORE_SUCCESS
                getView()?.handleVideoList(result, loadType)
            }

            override fun onFailed(e: Throwable?) {
                getView()?.hideLoading()
                val loadType = if (isRefresh) LoadType.TYPE_REFRESH_ERROR else LoadType.TYPE_LOAD_MORE_ERROR
                getView()?.handleVideoList(null, loadType)
                getView()?.onError(e)
            }
        })
    }

    override fun refresh() {
        page = 1
        isRefresh = true
        videoList()
    }

    override fun loadMore() {
        isRefresh = false
        videoList()
    }
}